﻿/* Copyright (C) 2007-2010 STS Soft

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; version 2 of the License.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */

using System;
using System.Collections.Generic;
using System.IO;
using STSdb.General.Collections;
using STSdb.General.Compression;

namespace STSdb.Persist
{
    //public class DictionaryIndexerPersist<T> : IIndexerPersist<T> where T : IComparable<T>//, ICloneable
    //{
    //    #region IIndexerPersist<T> Members

    //    public void Store(BinaryWriter writer, IIndexer<T> values)
    //    {
    //        int mapCapacity = (int)(0.618 * values.Count);
    //        List<T> map = new List<T>(mapCapacity);

    //        for (int i = 0; i < values.Count; i++)
    //        {
    //            T value = values[i];
    //            int index = map.BinarySearch(value);
    //            if (index < 0)
    //            {
    //                map.Insert(~index, value);
    //                if (map.Count > mapCapacity)
    //                    break;
    //            }
    //        }

    //        if (map.Count > mapCapacity)
    //        {
    //            writer.Write(false);
    //            (new PrimitiveIndexerPersist<T>()).Store(writer, values);
    //        }
    //        else
    //        {
    //            writer.Write(true);
    //            DeltaCompression.Helper helper = new DeltaCompression.Helper();
    //            List<long> ids = new List<long>(values.Count);
    //            for (int i = 0; i < values.Count; i++)
    //            {
    //                int id = map.BinarySearch(values[i]);
    //                ids.Add(id);
    //                helper.AddValue(id);
    //            }

    //            DeltaCompression.CoreCompress(writer, ids, helper);
    //            CountCompression.Serialize(writer, map.Count);
    //            (new PrimitiveIndexerPersist<T>()).Store(writer, new ListIndexer<T>(map));
    //        }
    //    }

    //    public void Load(BinaryReader reader, IIndexer<T> values)
    //    {
    //        if (!reader.ReadBoolean())
    //            (new PrimitiveIndexerPersist<T>()).Load(reader, values);
    //        else
    //        {
    //            List<long> ids = (List<long>)DeltaCompression.CoreDecompress(reader);
    //            T[] map = new T[(int)CountCompression.Deserialize(reader)];
    //            (new PrimitiveIndexerPersist<T>()).Load(reader, new ArrayIndexer<T>(map));

    //            for (int i = 0; i < values.Count; i++)
    //                values[i] = map[(int)ids[i]]; //TODO: Clone!
    //         }
    //    }

    //    #endregion
    //}
}
